/*
 * Ext GWT - Ext for GWT
 * Copyright(c) 2007-2009, Ext JS, LLC.
 * licensing@extjs.com
 * 
 * http://extjs.com/license
 */
package web.app.client;


import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.ButtonArrowAlign;
import com.extjs.gxt.ui.client.Style.IconAlign;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.View;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.HtmlContainer;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.Viewport;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.menu.Menu;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.user.client.ui.RootPanel;


public class AppView extends View {

  public static final String HEADER_PANEL = "app.header_panel";
  public static final String VIEWPORT = "viewport";
  public static final String CENTER_PANEL = "app.center_panel";
  private static final int BTN_HEIGHT = 55;
  private static final int BTN_WIDTH = 55;

  private Viewport viewport;
  private ContentPanel header;
  private LayoutContainer centerPanel;

  public AppView(Controller controller) {
    super(controller);
  }

  private void initUI() {
    viewport = new Viewport();
    viewport.setLayout(new BorderLayout());

    createHeader();
    createCenter();

    // registry serves as a global context
    Registry.register(VIEWPORT, viewport);
    Registry.register(HEADER_PANEL, header);
    Registry.register(CENTER_PANEL, centerPanel);

    RootPanel.get().add(viewport);
  }

  private void createHeader() {


    ToolBar toolBar = new ToolBar();

    StringBuffer sb = new StringBuffer();
    sb.append("<div style='width:250px'><img src='resources/images/logo.jpg' /><div style='font-size'>powered by ????</div></div>");

    toolBar.add(new HtmlContainer(sb.toString())  );


    for(Module m : AppConfig.getModules()){
        
        Button btn = new Button();
        //btn.setText(m.getName());
        btn.setArrowAlign(ButtonArrowAlign.BOTTOM);
        btn.setIcon(m.getIcon());
        btn.setHeight( BTN_HEIGHT );
        btn.setWidth( BTN_WIDTH );
        
        btn.setIconAlign(IconAlign.TOP);
        Menu menu = m.getMenu();
        
        

        if(menu!=null){
            btn.setMenu( menu );
        }

        toolBar.add(btn);
    }
    

    
    ContentPanel northPanel = new ContentPanel();
    northPanel.setStateful(false);

    BorderLayoutData data = new BorderLayoutData(LayoutRegion.NORTH, BTN_HEIGHT + 10);
    data.setMargins(new Margins());
    viewport.add(toolBar, data);
  }

  private void createCenter() {
    centerPanel = new LayoutContainer();
    centerPanel.setLayout(new FitLayout());

    BorderLayoutData data = new BorderLayoutData(LayoutRegion.CENTER);
    //data.setMargins(new Margins(5, 5, 5, 5));

    viewport.add(centerPanel, data);
  }

  protected void handleEvent(AppEvent event) {
    if (event.getType() == AppEvents.Init) {
      initUI();
      showModule( AppConfig.getModules().get(0) );
    }
  }

  public void showModule( Module m ){
      centerPanel.removeAll();
      centerPanel.add( m.getUI() );
      centerPanel.layout();
  }

}
